 package com.txby.platformApi.component.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jck.mybatis.mapper.MybatisBaseDaoCRUD;
import com.txby.pf.common.utils.R;
import com.txby.platformCore.entity.shop.BaseGoods;
import com.txby.platformCore.entity.shop.GoodsPic;
import com.txby.platformCore.entity.shop.GoodsPicRealation;
import com.txby.platformCore.entity.shop.GoodsStockNum;
import com.txby.platformCore.entity.shop.NrsGoods;
import com.txby.platformCore.entity.shop.OrderMealGoods;
import com.txby.platformCore.entity.shop.ServiceGoods;


@Service
public class WebServiceGoodsService extends MybatisBaseDaoCRUD {
 
	@Resource(name = "mybatisBaseDaoCRUD")
	public MybatisBaseDaoCRUD mBaseDao;
	/**
	 * 新增普通商品
	 * @param BaseGoods
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRED,timeout=36000,rollbackFor=Exception.class)
	public R saveBaseGoods(BaseGoods goods)  {
		try {
			String sql="select * from "+mBaseDao.tableName(BaseGoods.class)+" where name='"+goods.getName()+"' and shopsId='"+goods.getShopsId()+"' and goodsTypeId='"+goods.getGoodsTypeId()+"'";
			BaseGoods old=(BaseGoods) mBaseDao.findBySqlUniqueObject(sql, BaseGoods.class, null);
			 if(old!=null){
				 return R.ok().error("该商品已存在");
			 }
			BaseGoods savedBG =(BaseGoods) mBaseDao.saveObject2Object(goods);
			GoodsPic goodsPic = new GoodsPic();
//			goodsPic.setUrl(goods.getUrl());
			GoodsPic savedGoodsPic =(GoodsPic) mBaseDao.saveObject2Object(goodsPic);
			GoodsPicRealation goodsPicRealation = new GoodsPicRealation();
			goodsPicRealation.setGoodsId(savedBG.getId_());
			goodsPicRealation.setGoodsPicId(savedGoodsPic.getId_());
			goodsPicRealation.setGoodsTypeId(4l);;
			mBaseDao.saveObject2Object(goodsPicRealation);
			GoodsStockNum goodsStockNum = new GoodsStockNum();
			goodsStockNum.setGoodsTypeId(4l);
			goodsStockNum.setGoodsId(savedBG.getId_());
			goodsStockNum.setShopsId(goods.getShopsId());
//			goodsStockNum.setStockNum(goods.getStockNum());
//			goodsStockNum.setResidueNum(goods.getStockNum());
			goodsStockNum.setOrdersOutNum(0L);
			mBaseDao.saveObject2Object(goodsStockNum);
			
		} catch (Exception e) {
			return R.error(5000,e.getMessage());
		}
		return R.ok();
	}

	/** 
	* @Title: updateNrsGoods 
	* @Description:  
	* @param @param nrsGoods    设定文件 
	* @return void    返回类型 
	* @throws 
	*/
	@Transactional(propagation = Propagation.REQUIRED,timeout=36000,rollbackFor=Exception.class)
	public void updateNrsGoods(NrsGoods nrsGoods) {
		mBaseDao.updateObject(nrsGoods);
	}

	/** 
	* @Title: saveNrsGoods 
	* @Description:  
	* @param @param nrsGoods    设定文件 
	* @return void    返回类型 
	* @throws 
	*/
	@Transactional(propagation = Propagation.REQUIRED,timeout=36000,rollbackFor=Exception.class)
	public void saveNrsGoods(NrsGoods nrsGoods) {
		mBaseDao.saveObject(nrsGoods);
	}

	/** 
	* @Title: deleteNrsGoods 
	* @Description:  
	* @param @param nrsGoods    设定文件 
	* @return void    返回类型 
	* @throws 
	*/
	@Transactional(propagation = Propagation.REQUIRED,timeout=36000,rollbackFor=Exception.class)
	public void deleteNrsGoods(NrsGoods nrsGoods) {
		mBaseDao.deleteObject(nrsGoods);
	}

	/** 
	* @Title: updateBaseGoods 
	* @Description:  
	* @param @param baseGoods    设定文件 
	* @return void    返回类型 
	* @throws 
	*/
	@Transactional(propagation = Propagation.REQUIRED,timeout=36000,rollbackFor=Exception.class)
	public void updateBaseGoods(BaseGoods baseGoods) {
		mBaseDao.updateObject(baseGoods);
		
	}

	/** 
	* @Title: deleteBaseGoods 
	* @Description:  
	* @param @param baseGoods    设定文件 
	* @return void    返回类型 
	* @throws 
	*/
	@Transactional(propagation = Propagation.REQUIRED,timeout=36000,rollbackFor=Exception.class)
	public void deleteBaseGoods(BaseGoods baseGoods) {
		mBaseDao.deleteObject(baseGoods);
		
	}

	/** 
	* @Title: saveServiceGoods 
	* @Description:  
	* @param @param serviceGoods    设定文件 
	* @return void    返回类型 
	* @throws 
	*/
	@Transactional(propagation = Propagation.REQUIRED,timeout=36000,rollbackFor=Exception.class)
	public void saveServiceGoods(ServiceGoods serviceGoods) {
		mBaseDao.saveObject(serviceGoods);
		
	}

	/** 
	* @Title: updateServiceGoods 
	* @Description:  
	* @param @param serviceGoods    设定文件 
	* @return void    返回类型 
	* @throws 
	*/
	@Transactional(propagation = Propagation.REQUIRED,timeout=36000,rollbackFor=Exception.class)
	public void updateServiceGoods(ServiceGoods serviceGoods) {
		mBaseDao.updateObject(serviceGoods);
		
	}

	/** 
	* @Title: deleteServiceGoods 
	* @Description:  
	* @param @param serviceGoods    设定文件 
	* @return void    返回类型 
	* @throws 
	*/
	@Transactional(propagation = Propagation.REQUIRED,timeout=36000,rollbackFor=Exception.class)
	public void deleteServiceGoods(ServiceGoods serviceGoods) {
		mBaseDao.deleteObject(serviceGoods);
		
	}

	/** 
	* @Title: saveOrderMealGoods 
	* @Description:  
	* @param @param orderMealGoods    设定文件 
	* @return void    返回类型 
	* @throws 
	*/
	@Transactional(propagation = Propagation.REQUIRED,timeout=36000,rollbackFor=Exception.class)
	public void saveOrderMealGoods(OrderMealGoods orderMealGoods) {
		
		mBaseDao.saveObject(orderMealGoods);
	}

	/** 
	* @Title: updateOrderMealGoods 
	* @Description:  
	* @param @param orderMealGoods    设定文件 
	* @return void    返回类型 
	* @throws 
	*/
	@Transactional(propagation = Propagation.REQUIRED,timeout=36000,rollbackFor=Exception.class)
	public void updateOrderMealGoods(OrderMealGoods orderMealGoods) {
		mBaseDao.updateObject(orderMealGoods);
		
	}

	/** 
	* @Title: deleteOrderMealGoods 
	* @Description:  
	* @param @param orderMealGoods    设定文件 
	* @return void    返回类型 
	* @throws 
	*/
	@Transactional(propagation = Propagation.REQUIRED,timeout=36000,rollbackFor=Exception.class)
	public void deleteOrderMealGoods(OrderMealGoods orderMealGoods) {
		
		mBaseDao.deleteObject(orderMealGoods);
	}
}
